/**
 * 
 */
package com.archerdu.apply.service.impl;

import java.io.PrintWriter;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.archerdu.apply.dao.AdvertiseCatalogDAO;
import com.archerdu.apply.dao.AdvertiseDAO;
import com.archerdu.apply.model.Advertise;
import com.archerdu.apply.model.AdvertiseCatalog;
import com.archerdu.apply.service.AdvertiseCatalogManager;
import com.archerdu.base.dao.IBaseDao;
import com.archerdu.base.dao.TreeNode;
import com.archerdu.base.exception.BussinessException;
import com.archerdu.base.service.BaseManager;
import com.archerdu.commons.constant.DataConstant;
import com.archerdu.commons.constant.ErrorConstant;
import com.archerdu.commons.util.XMLUtil;

/**
 * @author archer
 * @version 1.0.0
 */
public class AdvertiseCatalogManagerImpl
        extends BaseManager<AdvertiseCatalog, String>
        implements AdvertiseCatalogManager {
    
    private static final long serialVersionUID = 4966611154968220095L;
    
    private AdvertiseCatalogDAO  AdvertiseCatalogDao;
    private AdvertiseDAO      advertiseDao;
    
    @Override
    public void delete(AdvertiseCatalog AdvertiseCate)
            throws Exception {
        if (null != AdvertiseCate && null != AdvertiseCate.getId()) {
            AdvertiseCate = AdvertiseCatalogDao.get(AdvertiseCate.getId());
            if (null != AdvertiseCate) {
                Advertise advertise = new Advertise();
                advertise.setCatalogId(AdvertiseCate.getId());
                // 目录下有内容， 不能删除
                if (advertiseDao.pageSearch(advertise, 1, 2).getTotal() != 0) {
                    throw new BussinessException(ErrorConstant.CATE_EXISTS_CONTENT);
                }
                // 有子目录， 不能删除
                if (AdvertiseCatalogDao.findByParentId(AdvertiseCate.getId()).size() != 0) {
                    throw new BussinessException(ErrorConstant.CATE_EXISTS_CHILD);
                }
                try {
                    super.delete(AdvertiseCate);
                }
                catch (Exception e) {
                    log.error(e.getMessage(), e);
                    throw new BussinessException(ErrorConstant.DELETE_ERROR);
                }
            }
            else {
                throw new BussinessException(ErrorConstant.CATE_NOT_EXISTS);
            }
        }
        else {
            throw new BussinessException(ErrorConstant.CATE_NOT_EXISTS);
        }
    }
    
    public AdvertiseCatalogDAO getAdvertiseCatalogDao() {
        return AdvertiseCatalogDao;
    }
    
    public void setAdvertiseCatalogDao(AdvertiseCatalogDAO dao) {
        AdvertiseCatalogDao = dao;
    }
    
    @Override
    protected IBaseDao<AdvertiseCatalog, String> getDao() {
        return (IBaseDao<AdvertiseCatalog, String>) AdvertiseCatalogDao;
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see com.archer.manager.AdvertiseCateManager#toXML(java.io.OutputStream)
     */
    public void toXML(PrintWriter pw) {
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement(AdvertiseCatalog.ROOT);
        root.addAttribute("id", "0");
        AdvertiseCatalog AdvertiseCate = new AdvertiseCatalog();
        AdvertiseCate.setId("0");
        loadChildren(root, AdvertiseCate);
        
        XMLUtil.save(pw, doc);
    }
    
    @Override
    public void add(AdvertiseCatalog entity)
            throws Exception {
        // 上级目录不存在
        if (entity.getParentId() == null) {
            throw new BussinessException(ErrorConstant.CATE_PARENT_NOT_EXISTS);
        }
        
        if (entity.getParentId().equals("0")) {
            entity.setCode("");
            entity.setLev(0);
        }
        else {
            AdvertiseCatalog parent = super.get(entity.getParentId());
            
            // 上级目录不存在
            if (parent == null) {
                throw new BussinessException(ErrorConstant.CATE_PARENT_NOT_EXISTS);
            }
            // 广告位不能添加子目录
            if (parent.getType().equals(DataConstant.ADVERTIES_CATE_TYPE_POSITION)) {
                throw new BussinessException(ErrorConstant.ADVERTISE_CATE_TYPE_POSITION);
            }
            // 广告组下只能添加广告位
            if (parent.getType().equals(DataConstant.ADVERTIES_CATE_TYPE_GROUP)
                    && !entity.getType().equals(DataConstant.ADVERTIES_CATE_TYPE_POSITION)) {
                throw new BussinessException(ErrorConstant.ADVERTISE_CATE_TYPE_GROUP);
            }
            
            entity.setCode(parent.getCode() + parent.getId());
            entity.setLev(parent.getLev() + 1);
        }
        super.add(entity);
    }
    
    public void loadChildren(Element ele, AdvertiseCatalog AdvertiseCate) {
        List<AdvertiseCatalog> list = getAdvertiseCatalogDao().findByParentId(AdvertiseCate.getId());
        if (list.size() > 0) {
            for (AdvertiseCatalog ca : list) {
                Element el = ele.addElement(TreeNode.ITEM);
                ca.toElement(el);
                loadChildren(el, ca);
            }
        }
        else {
            return;
        }
    }

    public AdvertiseDAO getAdvertiseDao() {
        return advertiseDao;
    }
    
    public void setAdvertiseDao(AdvertiseDAO advertiseDao) {
        this.advertiseDao = advertiseDao;
    }
}
